package com.lbs.tdrive.home;

import java.io.InputStream;


import com.lbs.tdrive.R;
import com.lbs.tdrive.RegisterActivity;
import com.lbs.tdrive.activity.Alerts;
import com.lbs.tdrive.activity.MyVehicles;
import com.lbs.tdrive.activity.Settings;
import com.lbs.tdrive.activity.TravelCost;
import com.lbs.tdrive.activity.Weather;
import com.lbs.view.MyHorizontalScrollView;
import com.lbs.view.MyHorizontalScrollView.SizeCallback;
import com.lbs.view.ViewUtils;
import com.lbs.view.adapter.MenuAdapter;


import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class HomeScreen extends Activity implements OnItemClickListener{
	private MyHorizontalScrollView scrollView;
	private View menu;
	private View app;
	private ImageView btnSlide;

	
	
    private MenuAdapter menuAdapter;
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LayoutInflater inflater = LayoutInflater.from(this);
        scrollView = (MyHorizontalScrollView) inflater.inflate(R.layout.horz_scroll_with_list_menu, null);
        setContentView(scrollView);

        menu = inflater.inflate(R.layout.horz_scroll_menu, null);
        app = inflater.inflate(R.layout.horz_scroll_app, null);
        ViewGroup tabBar = (ViewGroup) app.findViewById(R.id.tabBar);

        ListView listView_list = (ListView) menu.findViewById(R.id.list);
        menuAdapter = new MenuAdapter(this);
        listView_list.setAdapter(menuAdapter);
        listView_list.setOnItemClickListener(this);

        btnSlide = (ImageView) tabBar.findViewById(R.id.BtnSlide);
        btnSlide.setOnClickListener(new ClickListenerForScrolling(scrollView, menu));

        final View[] children = new View[] { menu, app };

        // Scroll to app (view[1]) when layout finished.
        int scrollToViewIdx = 1;
        scrollView.initViews(children, scrollToViewIdx, new SizeCallbackForMenu(btnSlide));
    }

    /**
     * Helper for examples with a HSV that should be scrolled by a menu View's width.
     */
    static class ClickListenerForScrolling implements OnClickListener {
        HorizontalScrollView scrollView;
        View menu;
        /**
         * Menu must NOT be out/shown to start with.
         */
        boolean menuOut = false;

        public ClickListenerForScrolling(HorizontalScrollView scrollView, View menu) {
            super();
            this.scrollView = scrollView;
            this.menu = menu;
        }

        @Override
        public void onClick(View v) {
          
            

            int menuWidth = menu.getMeasuredWidth();

            // Ensure menu is visible
            menu.setVisibility(View.VISIBLE);

            if (!menuOut) {
                // Scroll to 0 to reveal menu
                int left = 0;
                scrollView.smoothScrollTo(left, 0);
            } else {
                // Scroll to menuWidth so menu isn't on screen.
                int left = menuWidth;
                scrollView.smoothScrollTo(left, 0);
            }
            menuOut = !menuOut;
        }
    }

    /**
     * Helper that remembers the width of the 'slide' button, so that the 'slide' button remains in view, even when the menu is
     * showing.
     */
    static class SizeCallbackForMenu implements SizeCallback {
        int btnWidth;
        View btnSlide;

        public SizeCallbackForMenu(View btnSlide) {
            super();
            this.btnSlide = btnSlide;
        }

        @Override
        public void onGlobalLayout() {
            btnWidth = btnSlide.getMeasuredWidth();
            System.out.println("btnWidth=" + btnWidth);
        }

        @Override
        public void getViewSize(int idx, int w, int h, int[] dims) {
            dims[0] = w;
            dims[1] = h;
            final int menuIdx = 0;
            if (idx == menuIdx) {
                dims[0] = w - btnWidth;
            }
        }
    }
    


	@Override
	public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
		if (position==AppConstants.EXIT){
			finish();// TODO Auto-generated method stub
		}
		else if (position==AppConstants.MY_VEHICLES){
			 Intent intent = new Intent(this,MyVehicles.class);
	    	 startActivity(intent);
		}
		else if (position==AppConstants.ALERTS){
			 Intent intent = new Intent(this,Alerts.class);
	    	 startActivity(intent);
		}
		else if (position==AppConstants.SETTINGS){
			 Intent intent = new Intent(this,Settings.class);
	    	 startActivity(intent);
		}
		else if (position==AppConstants.TRAVEL_COST){
			 Intent intent = new Intent(this,TravelCost.class);
	    	 startActivity(intent);
		}
		else if (position==AppConstants.WEATHER){
			 Intent intent = new Intent(this,Weather.class);
	    	 startActivity(intent);
		}
		
	}
    
    
}
